﻿$(document).ready(function () {

    if (parseInt(nPermissions) >= 3) {
        $("#btnsave").click(function () {
            if ($.trim($(this).text()) == "Edit") {
                $('table[editcellcontent="true"]').InitialEditTableCell({ emptytext: "" });
                $(this).text("Done");
                $(".changed").addClass("status");
            }
            else {
                setTimeout(function () {
                    $("#btnsave").text("Edit");
                    $(".changed").removeClass("status");
                    SaveChangeItems();
                }, 50);
            }
        });
    } else {
        $("#btnsave").remove();
    }


    $('*[zoomable="true"]').InitialZoom();
});

function EditInline(row, sender) {
    var ischange = $(row).attr("changed");
    if ((typeof ischange == "undefined" || ischange == "") && $(sender).text() != "Done") {
        $(row).InitialEditTableCell({ emptytext: '', inline: true });
        $(row).addClass("editing");
        $(sender).text("Done");
    } else if (ischange == "true") {
        SaveInline(row, sender);
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    }
}

function SaveInline(row, sender) {

    if (typeof $(row).attr("changed") != "undefined" && $(row).attr("changed") == "true") {
        var ID = $.trim($(row).attr("cusid"));
        var Goods = $.trim($(row).children('[code="Goods"]').text());
        var GoodsEx = $.trim($(row).children('[code="GoodsEx"]').text());
        var HsGoods = $.trim($(row).children('[code="HsGoods"]').text());
        var HsGoodsEx = $.trim($(row).children('[code="HsGoodsEx"]').text());
        var Name = $.trim($(row).children('[code="Name"]').text());
        var Code = $.trim($(row).children('[code="Code"]').text());
        var Address = $.trim($(row).children('[code="Address"]').text());
        var Pic = $.trim($(row).children('[code="Pic"]').text());
        var Position = $.trim($(row).children('[code="Position"]').text());
        var Tel = $.trim($(row).children('[code="Tel"]').text());
        var Mobile = $.trim($(row).children('[code="Mobile"]').text());
        var Email = $.trim($(row).children('[code="Email"]').text());
        var Social = $.trim($(row).children('[code="Social"]').text());
        var pol = $.trim($(row).children('[code="Pol"]').text());
        var Routers = $.trim($(row).children('[code="Routers"]').text());
        var Vols = $.trim($(row).children('[code="Vols"]').text());
        var report = $.trim($(row).children('[code="Report"]').text());
        var Goods = "";
        var ListType = 1;
        var UserID = -1;
        var Description = "";
        var Sort = "";
        var Owner = 0;
        var Duplicate = 0;
        var Level = "";
        var AutoLevel = 0;
        var LastUpdate = "";



        if ($.trim(Name) == "") {
            alert("Tên khách hàng không được để trống!");
            return;
        }

        $.ajax({
            type: 'POST',
            url: "Handlers/UpdateCustomerRelation.ashx",
            data: {
                ID: ID,
                Goods: Goods,
                Name: Name,
                Code: Code,
                Address: Address,
                Tel: Tel,
                Mobile: Mobile,
                Email: Email,
                Social: Social,
                Pic: Pic,
                Position: Position,
                Routers: Routers,
                Vols: Vols,
                Pol: pol,
                Report: report
            },
            success: function (data) {
                if (data != null && data == "NAME_BLANK") {
                    alert("Tên khách hàng không được để trống!");
                } else {
                    $(row).removeAttr("changed");
                    $(row).DisableEditTableCell();
                    $(sender).text("Edit");
                }
            },
            dataType: "json"
        });
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
    }
}

function SaveChangeItems() {
    var rowchanges = $('tr[changed="true"]');
    $('table[editcellcontent="true"]').DisableEditTableCell();
    if ($(rowchanges).length == 0) {
        return;
    }
    var temp = "";
    var items = new Array();
    var changecells = new Array();
    var _cell;
    var doSave = true;
    $.each(rowchanges, function (i, row) {
        var ID = $.trim($(row).attr("cusid"));
        var Name = $.trim($(row).children('[code="Name"]').text());
        var Code = $.trim($(row).children('[code="Code"]').text());
        var Address = $.trim($(row).children('[code="Address"]').text());
        var Pic = $.trim($(row).children('[code="Pic"]').text());
        var Position = $.trim($(row).children('[code="Position"]').text());
        var Tel = $.trim($(row).children('[code="Tel"]').text());
        var Mobile = $.trim($(row).children('[code="Mobile"]').text());
        var Email = $.trim($(row).children('[code="Email"]').text());
        var Social = $.trim($(row).children('[code="Social"]').text());
        var pol = $.trim($(row).children('[code="Pol"]').text());
        var Routers = $.trim($(row).children('[code="Routers"]').text());
        var Vols = $.trim($(row).children('[code="Vols"]').text());
        var report = $.trim($(row).children('[code="Report"]').text());
        var Goods = $.trim($(row).children('[code="Goods"]').text());
        var GoodsEx = $.trim($(row).children('[code="GoodsEx"]').text());
        var HsGoods = $.trim($(row).children('[code="HsGoods"]').text());
        var HsGoodsEx = $.trim($(row).children('[code="HsGoodsEx"]').text());
        var ListType = 1;
        var UserID = -1;
        var Description = "";
        var Sort = "";
        var Owner = 0;
        var Duplicate = 0;
        var Level = "";
        var AutoLevel = 0;
        var LastUpdate = "";

        if (Name == "") {
            alert("Tên khách hàng không đúng!");
            $(row).children('[code="Name"]').effect("highlight", {}, 5000);
            doSave = false;
            return;
        }

        items.push("{'ID':'" + ID
                    + "','Name':'" + Encoder.htmlEncode(Name)
                    + "','Code':'" + Encoder.htmlEncode(Code)
                    + "','Address':'" + Encoder.htmlEncode(Address)
                    + "','Pic':'" + Encoder.htmlEncode(Pic)
                    + "','Position':'" + Encoder.htmlEncode(Position)
                    + "','Tel':'" + Encoder.htmlEncode(Tel)
                    + "','Mobile':'" + Encoder.htmlEncode(Mobile)
                    + "','Email':'" + Encoder.htmlEncode(Email)
                    + "','Social':'" + Encoder.htmlEncode(Social)
                    + "','Pol':'" + Encoder.htmlEncode(pol)
                    + "','Routers':'" + Encoder.htmlEncode(Routers)
                    + "','Vols':'" + Encoder.htmlEncode(Vols)
                    + "','Report':'" + Encoder.htmlEncode(report)
                    + "','Goods':'" + Encoder.htmlEncode(Goods)
                    + "','GoodsEx':'" + Encoder.htmlEncode(GoodsEx)
                    + "','HsGoods':'" + Encoder.htmlEncode(HsGoods)
                    + "','HsGoodsEx':'" + Encoder.htmlEncode(HsGoodsEx)
                    + "','ListType':'" + 1
                    + "','UserID':'" + 1
                    + "','Description':'" + ""
                    + "','Sort':'" + 0
                    + "','Owner':'" + 0
                    + "','Duplicate':'" + 0
                    + "','Level':'" + 0
                    + "','AutoLevel':'" + 0
                    + "','LastUpdate':'" + new Date().toDateString()
                    + "'}");
    });
    if (!doSave) {
        $("#btnsave").text("Done");
        $(".changed").addClass("status");
        return;
    }
    $.ajax({
        type: 'POST',
        url: "Handlers/UpdateCustomerRelation.ashx",
        data: { items: "[" + items.toString() + "]", version: "2", page: "customerstore" },
        success: function (data) {
            if (data != null && data == "NAME_BLANK") {
                alert("Tên khách hàng không được để trống!");
            } else {
                $('tr[changed="true"]').removeAttr("changed");
                //                $.each(rowchanges, function (i, row) {
                //                    $(row).removeAttr("changed");
                //                    
                //                });
            }
        },
        dataType: "json"
    });
}